﻿using APPFrameworkStudy.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace APPFrameworkStudy.Domain
{
    /// <summary>
    /// 订单领域模型，这里提供最小粒度的业务处理，不涉及数据访问，供服务层调用
    /// 服务层根据用例将小粒度业务组合为粗粒度服务供表现层调用
    /// </summary>
    public class Order : IEntity
    {
        public int ID { get; set; }
        public string Number { get; set; }
        public decimal ShipAmount { get; set; }
        public DateTime CreateTime { get; set; }

        private List<Product> _orderItems = new List<Product>();
        /// <summary>
        /// 定单明细
        /// </summary>
        public List<Product> OrderItems
        {
            get { return _orderItems; }
            set { _orderItems = value; }
        }

        public override string ToString()
        {
            return "ID:" + ID + "，订单总额:" + ShipAmount + "，CreateTime:" + CreateTime.ToString();
        }

        /// <summary>
        /// 计算定单金额
        /// </summary>
        /// <returns></returns>
        public decimal GetOrderAmount()
        {
            decimal amount = 0;
            //计算商品金额
            foreach (var item in OrderItems)
            {
                amount += item.GetAmount();
            }
            //加上运费
            amount += ShipAmount;
            return amount;
        }
    }
}
